import {Component, ElementRef, OnInit, Renderer2, ViewChild} from '@angular/core';
import {NzContextMenuService, NzMessageService, NzModalService} from 'ng-zorro-antd';
import {AppService} from '../../../app.service';
import {UploadData, XcUploadTool} from '../../../common/xc-upload.tool';
import {FileManagementService} from '../file-management.service';
import {CommonTool} from '../../../common/common.tool';
import {environment} from '../../../../environments/environment';
import {MusicPlayerComponent} from '../../../common/music-player/music-player.component';
import {TableFindModel} from '../../../common/table-find/table-find.model';
import {LoginDto} from '../../../basic/basic.model';
import {ActivatedRoute, Router} from '@angular/router';
import Viewer from 'viewerjs';
import {DictService} from '../../../common/service/dict.service';
import {BUTTON_CODE} from '../../../config/button-code';
import {CreateFolderComponent} from '../create-folder/create-folder.component';

@Component({
  selector: 'app-file-management',
  templateUrl: './file.component.html',
  styleUrls: ['./file.component.scss']
})
export class FileComponent implements OnInit {

  @ViewChild(MusicPlayerComponent, {static: true})
  public musicPlayerComponent: MusicPlayerComponent;
  public commonTool = CommonTool;
  public recommendMovie: Array<{ name, value }> = [];
  public tableFindModel: TableFindModel;
  public tableFindModel1: TableFindModel;
  public tableFindModel2: TableFindModel;
  public edit = {isFile: true, name: '', id: '', isVisible: false, okDisabled: false, nameStatus: ''};
  public share = {
    isVisible: false,
    fileIds: [],
    folderIds: [],
    okDisabled: false,
    needCode: '0',
    validTime: '0',
    remark: ''
  };
  public selectFolder = {folderId: '', isVisible: false, type: '', okText: ''};


  public shareUrl = {
    url: '',
    isVisible: false,
    drawCode: ''
  };
  public showMusicPlay = false;
  public pictureFiles: Array<any>;
  public showPicture = false;
  public videoPlay = {url: '', isVisible: false};
  public upload = {isVisible: false};
  public package = {
    isVisible: false,
    nameStatus: '',
    fileIds: [],
    folderIds: [],
    okDisabled: true,
    name: ''
  };
  public renewalSize = 10485760; // 文件读取大小
  public count; // 计数
  public viewer;
  public imgConfig = {};
  public cdn = {isVisible: false, url: ''};

  public compressUrl = '?open=0&w=30&h=50';
  public compressUrl1 = '?open=0';

  @ViewChild('editFolderName', {static: true}) editFolderName: ElementRef;
  @ViewChild('shareRemark', {static: true}) shareRemark: ElementRef;
  public userList = [];
  public fileLength = 0;
  public folderLength = 0;
  @ViewChild('createFolder', {static: true})
  public createFolderComponent: CreateFolderComponent;

  @ViewChild('createFolder1', {static: true})
  public createFolderComponent1: CreateFolderComponent;


  constructor(private nzContextMenuService: NzContextMenuService,
              public fileManagementService: FileManagementService,
              private modalService: NzModalService,
              public appService: AppService,
              public router: Router,
              public activateRoute: ActivatedRoute,
              private message: NzMessageService,
              public renderer: Renderer2,
              public dictService: DictService,
              public elementRef: ElementRef) {

    // 获取字典信息
    this.dictService.getCacheAndDictListByType('fileIcon').subscribe(res => {
      for (const dict of res) {
        this.imgConfig[dict.name] = dict.value;
      }
    });
    this.dictService.getCacheAndDictListByType('recommendMovie').subscribe(res => {
      this.recommendMovie = res;
    });
    this.count = 0;
    this.tableFindModel = new TableFindModel();
    this.tableFindModel1 = new TableFindModel();
    this.tableFindModel2 = new TableFindModel();
    this.pictureFiles = [];
    this.fileManagementService.getMyAppUserList().subscribe(res => {
      this.userList = res;
      const folderId = this.activateRoute.snapshot.queryParams['folderId'];
      if (folderId) {
        this.fileManagementService.getParentFolderList(folderId).subscribe(folders => {
          this.init(folderId, folders);
        });
      } else {
        this.init(folderId, []);
      }
    });
    this.uploadInit();
  }

  // 初始化
  ngOnInit(): void {
    const that = this;
    if (window.history && window.history.pushState) {
      window.onpopstate = (e) => {
        const url = decodeURI(e['target']['location'].href);
        const data = CommonTool.getUrlParam(url);
        if (data['folderId']) {
          that.tableFindModel.getData({folderId: data['folderId']}, null);
        } else {
          that.tableFindModel.getData({folderId: null}, null);
        }
      };
    }
    const drop = this.elementRef.nativeElement.querySelector('#upload-file');
    // 拖动文件上传
    this.renderer.listen(drop, 'dragover', (event) => {
      // 取消默认浏览器拖拽效果
      event.preventDefault();
    });
    this.renderer.listen(drop, 'drop', (e) => {
      const dataTransfer = e.dataTransfer;
      if (dataTransfer && dataTransfer.files && dataTransfer.files.length) {
        e.preventDefault();
        XcUploadTool.handleFileDirectory(dataTransfer).then((files) => {
          that.uploadFolderFile(files, that.tableFindModel.formInfo['folderId']);
        });
      }
    });
  }

  public modalOpen(key) {
    this[key].nativeElement.focus();
  }

  /**
   * 初始化
   */
  public init(folderId, folders) {
    const myThis = this;
    const COMMON_TYPE = TableFindModel.COMMON_TYPE;
    const disabledFun = (formInfo, that) => {
      let bool = true;
      that.dataModels.forEach(dataModel => {
        if (dataModel.selected) {
          bool = false;
        }
      });
      return bool;
    };
    const labels = [{name: '全部', folderId: ''}];
    for (const folder of folders) {
      labels.push({
        name: folder.name,
        folderId: folder.id
      });
    }
    this.tableFindModel.initAll({
      finds: [
        {
          name: '文件名',
          field: 'name',
          input: {
            maxLength: 255
          }
        }
      ],
      buttons: [
        {
          name: '创建目录',
          type: COMMON_TYPE.button.BUTTON,
          bAuthority: BUTTON_CODE.b_folder_add,
          confirmFun: (formInfo) => {
            myThis.createFolderComponent.create(formInfo['folderId']);
          }
        },
        {
          name: '上传文件',
          bAuthority: BUTTON_CODE.b_folder_file_upload,
          type: COMMON_TYPE.button.BUTTON,
          confirmFun: () => {
            myThis.selectFile();
          }
        },
        {
          name: '上传文件夹',
          bAuthority: BUTTON_CODE.b_folder_file_upload,
          type: COMMON_TYPE.button.BUTTON,
          confirmFun: () => {
            myThis.uploadFolder();
          }
        },
        {
          name: '音乐播放器',
          bAuthority: BUTTON_CODE.b_music_play,
          type: COMMON_TYPE.button.BUTTON,
          confirmFun: (formInfo, that) => {
            myThis.showMusicPlay = true;
            myThis.handleMp3(null);
          }
        },
        {
          name: '分享',
          bAuthority: BUTTON_CODE.b_folder_file_share,
          type: COMMON_TYPE.button.BUTTON,
          disabledFun,
          confirmFun: (formInfo, that) => {
            myThis.shareRemark.nativeElement.focus();
            const selectedArray = that.getSelectedArray();
            const fileIds = [];
            const folderIds = [];
            selectedArray.forEach(dataModel => {
              if (dataModel.isFile) {
                fileIds.push(dataModel.data.id);
              } else {
                folderIds.push(dataModel.data.id);
              }
            });
            myThis.share.isVisible = true;
            myThis.share.fileIds = fileIds;
            myThis.share.folderIds = folderIds;
          }
        },
        {
          name: '复制',
          bAuthority: BUTTON_CODE.b_folder_file_copy,
          type: COMMON_TYPE.button.BUTTON,
          disabledFun,
          confirmFun: (formInfo, that) => {
            myThis.selectFolder = {folderId: '', isVisible: true, type: 'copy', okText: '复制'};
            myThis.tableFindModel1 = myThis.fileManagementService.selectFolder(myThis.imgConfig, myThis.createFolderComponent1, (label) => {
              myThis.selectFolder.folderId = label.folderId;
            }, {isMyOf: false, userList: myThis.userList});
          }
        },
        {
          name: '移动',
          type: COMMON_TYPE.button.BUTTON,
          disabledFun,
          bAuthority: BUTTON_CODE.b_folder_file_move,
          confirmFun: (formInfo, that) => {
            myThis.selectFolder = {folderId: '', isVisible: true, type: 'move', okText: '移动'};
            myThis.tableFindModel1 = myThis.fileManagementService.selectFolder(myThis.imgConfig, myThis.createFolderComponent1, (label) => {
              myThis.selectFolder.folderId = label.folderId;
            }, {isMyOf: true});
          }
        },
        {
          name: '打包',
          type: COMMON_TYPE.button.BUTTON,
          disabledFun,
          bAuthority: BUTTON_CODE.b_folder_file_pack,
          confirmFun: (formInfo, that) => {
            const selectedArray = that.getSelectedArray();
            const fileIds = [];
            const folderIds = [];
            selectedArray.forEach(dataModel => {
              if (dataModel.isFile) {
                fileIds.push(dataModel.data.id);
              } else {
                folderIds.push(dataModel.data.id);
              }
            });
            myThis.package.isVisible = true;
            myThis.package.fileIds = fileIds;
            myThis.package.folderIds = folderIds;
          }
        },
        {
          name: '删除',
          type: COMMON_TYPE.button.BUTTON,
          disabledFun,
          bAuthority: BUTTON_CODE.b_folder_file_delete,
          confirmFun(formInfo, that) {
            myThis.delete(myThis.tableFindModel.getSelectedArray());
          }
        }
      ],
      breadcrumb: {
        breadcrumbFun: (label, i, that) => {
          if (i + 1 < labels.length) {
            labels.splice(i + 1, labels.length);
          }
          that.getData({folderId: labels[i].folderId}, null);
        },
        labels: [] // 面包屑
      },
      tables: [
        {name: '', field: 'checkbox', type: COMMON_TYPE.table.CHECKBOX},
        {
          name: '', field: 'imageUrl', type: COMMON_TYPE.table.IMAGE,
          tdStyle: {cursor: 'pointer'},
          confirmFun: (dataModel, table, that) => {
            if (!dataModel['isFile']) {
              const label = {name: dataModel.data.name, folderId: dataModel.data.id};
              labels.push(label);
              that.getData({folderId: dataModel.data.id}, null);
            } else {
              myThis.showPicture = true;
              myThis.openFile(dataModel.data);
            }
          },
          initFun(dataModel, that) {// dataModel：初始化的数据，that:当前对象
            if (dataModel['isFile']) {
              // 获取后缀
              const suffix = CommonTool.getSuffix(dataModel.data.name);
              if (myThis.fileManagementService.verifyImageSuffix(suffix)) {
                dataModel.data['imageUrl'] = dataModel.data['url'] + myThis.compressUrl;
                myThis.pictureFiles.push(dataModel.data);
              } else {
                const i = dataModel.data.name.lastIndexOf('.');
                const fileSuffix = dataModel.data.name.substring(i + 1, dataModel.data.name.length);
                if (myThis.imgConfig['disk_file_icon_' + fileSuffix]) {
                  dataModel.data['imageUrl'] = myThis.imgConfig['disk_file_icon_' + fileSuffix];
                } else {
                  dataModel.data['imageUrl'] = myThis.imgConfig['disk_icon_file'];
                }
              }
            }
          }
        },
        {
          name: '文件名',
          field: 'name',
          type: COMMON_TYPE.table.TITLE,
          isSort: true,
          tdStyle: {cursor: 'pointer'},
          confirmFun: (dataModel, table, that) => {
            if (!dataModel['isFile']) {
              const label = {name: dataModel.data.name, folderId: dataModel.data.id};
              labels.push(label);
              that.getData({folderId: dataModel.data.id}, null);
            } else {
              myThis.openFile(dataModel.data);
            }
          }
        },
        {
          name: '', field: 'createTime', type: COMMON_TYPE.table.DESCRIPTION,
          confirmFun: (dataModel, table, that) => {
            if (!dataModel['isFile']) {
              const label = {name: dataModel.data.name, folderId: dataModel.data.id};
              labels.push(label);
              that.getData({folderId: dataModel.data.id}, null);
            }
          }
        },
        {
          name: '', field: 'size', type: COMMON_TYPE.table.DESCRIPTION,
          tdStyle: {cursor: 'pointer'},
          confirmFun: (dataModel) => {
            if (!dataModel['isFile']) {
              if (dataModel.data['size'] === '计算') {
                myThis.appService.setLoading(true);
                myThis.fileManagementService.getUserFolderSize(dataModel.data['id']).subscribe(res => {
                  dataModel.data['size'] = CommonTool.getSpace(res.size);
                  myThis.appService.setLoading(false);
                });
              }
            }
          }
        },
        {
          name: '', field: '', type: COMMON_TYPE.table.OPERATE,
          initFun(dataModel, that) {// data：初始化的数据，that:全局this
            if (!dataModel['isFile']) {
              dataModel['cdn'] = false;
              dataModel['download'] = false;
            }
          },
          operates: [
            {
              name: '下载',
              field: 'download',
              bAuthority: BUTTON_CODE.b_file_download,
              confirmFun(dataModel, that) {
                location.href = dataModel.data['url'];
              }
            },
            {
              name: '编辑',
              field: 'edit',
              bAuthority: BUTTON_CODE.b_folder_file_edit,
              confirmFun(dataModel, that) {
                myThis.editFolderName.nativeElement.focus();
                myThis.edit = {
                  id: dataModel.data.id,
                  name: dataModel.data.name,
                  isVisible: true,
                  okDisabled: false,
                  nameStatus: '',
                  isFile: dataModel['isFile']
                };
              }
            },
            {
              name: 'cdn',
              field: 'cdn',
              bAuthority: BUTTON_CODE.b_file_cdn,
              confirmFun(dataModel, that) {
                myThis.createCdnOk(dataModel.data.id);
              }
            },
            {
              name: '删除',
              field: 'delete',
              bAuthority: BUTTON_CODE.b_folder_file_delete,
              confirmFun(dataModel, that) {
                myThis.delete([dataModel]);
              }
            }
          ]
        }
      ],
      initDataFun(formInfo, sortPage, callback, that) {// 数据处理回调 config：配置，dataModels：数据，that：当前对象
        myThis.appService.setLoading(true);
        const req = {
          sortField: sortPage.sortField,
          sortRule: sortPage.sortRule,
          likeFields: ['name'],
          folderId: formInfo['folderId']
        };
        for (const find of that.config.finds) {
          req[find.field] = formInfo[find.field];
        }
        myThis.fileManagementService.getUserFolderFile(req).subscribe(res => {
          myThis.appService.setLoading(false);
          const array = [];
          myThis.pictureFiles = [];
          myThis.fileLength = res['files'].length;
          myThis.folderLength = res['folders'].length;
          // 处理目录数据
          const tokenInfo = new LoginDto(JSON.parse(CommonTool.getCookie(environment.tokenKey)));
          for (const arr of res['folders']) {
            if (arr.userId === tokenInfo.userId) {
              arr.createTime = '我的 | ' + arr.createTime;
            }
            for (const user of myThis.userList) {
              if (user.userId === arr.userId && tokenInfo.userId !== user.userId) {
                arr.createTime = user.nickName + ' | ' + arr.createTime;
              }
            }
            arr.size = '计算';
            arr['imageUrl'] = myThis.imgConfig['disk_icon_folder'];
            array.push({data: arr, isFile: false});
          }
          // 处理文件数据
          for (const arr of res['files']) {
            arr.size = CommonTool.getSpace(arr.size);
            if (arr.userId === tokenInfo.userId) {
              arr.createTime = '我的 | ' + arr.createTime;
            }
            for (const user of myThis.userList) {
              if (user.userId === arr.userId && tokenInfo.userId !== user.userId) {
                arr.createTime = user.nickName + ' | ' + arr.createTime;
              }
            }
            array.push({
              data: arr,
              isFile: true
            });
          }
          // 设置labels
          that.config.breadcrumb.labels = labels;
          // 跳转记录
          if (formInfo['folderId'] !== null) {
            const url = CommonTool.analysisParam('/home/fileManage/file', {folderId: formInfo['folderId']});
            myThis.router.navigateByUrl(url);
          } else {
            myThis.router.navigateByUrl('/home/fileManage/file');
          }
          callback(array, {size: array.length});
        });
      },
      openPaging: false,
      frontSort: false
    }, null, {folderId});
  }

  /**
   * 打开文件
   */
  public openFile(data) {
    const suffix = CommonTool.getSuffix(data.name);
    if (suffix === '.mp3') {
      this.handleMp3(data.id);
    } else if (data['url']) {
      this.handleFile(data);
    } else {
      this.handleFile(data);
    }
  }

  /**
   * 处理mp3
   */
  public handleMp3(id) {
    this.musicPlayerComponent.setPlayList([]);
    this.tableFindModel.dataModels.forEach((ref) => {
      const suffix = CommonTool.getSuffix(ref.data['name']);
      if (suffix === '.mp3' && !ref.data['url']) {
        const playId = this.musicPlayerComponent.addMusic({
          name: ref.data['name'],
          url: ref.data['url']
        });
        if (id === ref.data['id']) {
          this.showMusicPlay = true;
          this.musicPlayerComponent.play(playId);
        }
      } else if (suffix === '.mp3') {
        const playId = this.musicPlayerComponent.addMusic({
          name: ref.data['name'],
          url: ref.data['url']
        });
        if (id === ref.data['id']) {
          this.showMusicPlay = true;
          this.musicPlayerComponent.play(playId);
        }
      }
    });
  }

  /**
   * 处理文件
   */
  public handleFile(data) {
    const that = this;
    const suffix = CommonTool.getSuffix(data.name);
    if (suffix === '.mp4') {
      this.videoPlay.isVisible = true;
      this.videoPlay.url = data['url'];
    } else if (this.fileManagementService.verifyImageSuffix(suffix)) {
      if (!this.viewer) {
        this.viewer = new Viewer(document.getElementById('image'), {
          initialViewIndex: 0,
          url(image) {
            return image.src.split('?')[0] + that.compressUrl1;
          }
        });
      } else {
        this.viewer.update();
      }
      for (let i = 0; i < this.pictureFiles.length; i++) {
        if (data['id'] === this.pictureFiles[i]['id']) {
          this.viewer.view(i);
        }
      }
      this.viewer.show();
    }
  }

  /**
   * 打包提交
   */
  public packageOk(pack) {
    this.appService.setLoading(true);
    this.fileManagementService.createUserFilePackage({
      fileIds: pack.fileIds,
      folderIds: pack.folderIds,
      name: pack.name,
      folderId: this.tableFindModel.formInfo['folderId'],
      format: 'zip'
    }).subscribe(res => {
      pack.name = '';
      pack.isVisible = false;
      this.tableFindModel.getData(null, null);
      this.message.success('打包成功！');
    });
  }

  /**
   * 移动复制文件及文件夹
   */
  public copyMoveFolderFile() {
    this.appService.setLoading(true);
    const selectedArray = this.tableFindModel.getSelectedArray();
    const fileIds = [];
    const folderIds = [];
    selectedArray.forEach(dataModel => {
      if (dataModel.isFile) {
        fileIds.push(dataModel.data.id);
      } else {
        folderIds.push(dataModel.data.id);
      }
    });
    const req = {
      fileIds,
      folderIds,
      folderId: this.selectFolder.folderId
    };
    if (this.selectFolder.type === 'copy') {
      this.fileManagementService.createUserFolderFile(req).subscribe(res => {
        this.selectFolder.isVisible = false;
        this.tableFindModel.getData(null, null);
        this.message.success('复制成功！');
      });
    } else {
      this.fileManagementService.updateUserFolderFile(req).subscribe(res => {
        this.selectFolder.isVisible = false;
        this.tableFindModel.getData(null, null);
        this.message.success('移动成功！');
      });
    }
  }

  /**
   * 验证文件目录输入框
   * 参数 data 数据
   * 参数 field 字段
   */
  public folderInput(data, field) {
    if (!CommonTool.notNull(data[field])) {
      data[field + 'Status'] = 'error';
      data.okDisabled = true;
      return;
    }
    const pattern = new RegExp('[/\\\\:*?"<>|]');
    if (pattern.test(data[field])) {
      data[field + 'Status'] = 'error';
      data.okDisabled = true;
    } else {
      data.okDisabled = false;
      data[field + 'Status'] = '';
    }
  }

  /**
   * 修改文件名称
   */
  public updateFileFolder(edit) {
    this.appService.setLoading(true);
    if (edit.isFile) {
      this.fileManagementService.updateUserFile(edit.id, {name: edit.name}).subscribe(() => {
        edit.isVisible = false;
        this.tableFindModel.getData(null, null);
        this.message.success('修改成功！');
      });
    } else {
      this.fileManagementService.updateUserFolder(edit.id, {name: edit.name}).subscribe(() => {
        edit.isVisible = false;
        this.tableFindModel.getData(null, null);
        this.message.success('修改成功！');
      });
    }

  }

  /**
   * 共享文件关闭弹窗
   */
  public fileShareCancel() {
    this.share = {
      isVisible: false,
      fileIds: [],
      folderIds: [],
      okDisabled: false,
      needCode: '0',
      validTime: '0',
      remark: ''
    };
  }

  /**
   * 分享文件
   */
  public fileShare() {
    this.appService.setLoading(true);
    this.fileManagementService.createUserShare({
      fileIds: this.share.fileIds,
      folderIds: this.share.folderIds,
      needCode: this.share.needCode,
      validTime: this.share.validTime,
      remark: this.share.remark
    }).subscribe((res) => {
      this.shareUrl.url = location.origin + '/#/share/' + res.code;
      this.shareUrl.isVisible = true;
      this.shareUrl.drawCode = res.drawCode;
      this.fileShareCancel();
      this.appService.setLoading(false);
    });
  }

  /**
   * 确认创建弹窗
   */
  public createFolderOk() {
    this.tableFindModel.getData(null, null);
  }

  /**
   * 确认创建弹窗
   */
  public createFolderOk1() {
    this.tableFindModel1.getData(null, null);
  }

  /**
   * 创建cdn确认回调
   */
  public createCdnOk(fileId) {
    this.appService.setLoading(true);
    this.fileManagementService.createCdnUrl(fileId).subscribe(res => {
      this.cdn.isVisible = true;
      this.cdn.url = res.cdnUrl;
      this.appService.setLoading(false);
    });
  }

  /**
   * 删除
   * 参数 data 数据
   */
  public delete(dataModels) {
    let index = 0;
    this.modalService.confirm({
      nzTitle: '确认要删除文件/目录吗?',
      nzContent: '<b style="color: red;">文件/目录删除后将无法找回哦！</b>',
      nzOkText: '确认',
      nzOkType: 'danger',
      nzOnOk: () => {
        this.appService.setLoading(true);
        for (const dataModel of dataModels) {
          if (dataModel.isFile) {
            this.fileManagementService.deleteFile(dataModel.data.id).subscribe(res => {
              index++;
              if (index === dataModels.length) {
                this.message.success('删除成功！');
                this.tableFindModel.getData(null, null);
              }
            });
          } else {
            this.fileManagementService.deleteFolder(dataModel.data.id).subscribe(res => {
              index++;
              if (index === dataModels.length) {
                this.message.success('删除成功！');
                this.tableFindModel.getData(null, null);
              }
            });
          }
        }
      },
      nzCancelText: '取消'
    });
  }

  /**
   * 视频播放
   */
  public playMovie(url) {
    this.videoPlay.isVisible = true;
    this.videoPlay.url = url;
  }

  /**
   * 上传文件初始化
   */
  public uploadInit() {
    const COMMON_TYPE = TableFindModel.COMMON_TYPE;
    this.tableFindModel2.initConfig({
      tables: [
        {name: '名称', field: 'name', type: COMMON_TYPE.table.TEXT},
        {name: '大小', field: 'size', type: COMMON_TYPE.table.TEXT},
        {name: '进度', field: 'progress', type: COMMON_TYPE.table.PROGRESS},
        {
          name: '操作', field: '', type: COMMON_TYPE.table.OPERATE,
          initFun(dataModel, that) {// data：初始化的数据，that:全局this
            dataModel['operateCancel'] = true;
            dataModel['operateRetry'] = false;
            if (dataModel['upload'].stopUpload) {
              dataModel['operateStart'] = true;
              dataModel['operateStop'] = false;
            } else {
              dataModel['operateStart'] = false;
              dataModel['operateStop'] = true;
            }
          },
          operates: [
            {
              name: '暂停', field: 'operateStop', confirmFun(dataModel, table, that) {
                dataModel['operateStart'] = true;
                dataModel['operateStop'] = false;
                dataModel['upload'].stop();
              }
            },
            {
              name: '开始', field: 'operateStart', confirmFun(dataModel, table, that) {
                dataModel['operateStart'] = false;
                dataModel['operateStop'] = true;
                dataModel['upload'].start();
              }
            },
            {
              name: '重试', field: 'operateRetry', confirmFun(dataModel, table, that) {
                dataModel['operateStop'] = true;
                dataModel['operateRetry'] = false;
                dataModel['upload'].retry();
              }
            },
            {
              name: '取消', field: 'operateCancel', confirmFun(dataModel, table, that) {
                dataModel['upload'].stop();
                that.dataModels.splice(dataModel.data.index, 1);
                const array = [];
                that.dataModels.forEach((model, index) => {
                  model.data.index = index;
                  array.push(model);
                });
                that.dataModels = array;
                if (array.length > 0) {
                  that.dataModels[dataModel.data.index]['upload'].start();
                }
              }
            }
          ]
        }
      ],
      paging: {
        current: 1, // 默认显示第几页
        size: 5, // 默认每页多少条数据
        total: 0, // 总数
        limits: [] // 每页多少数据的可选参数
      },
      finds: [],
      buttons: [],
      initDataFun: null,
      openPaging: true,
      frontSort: false,
      openFind: false
    });
  }

  /**
   * 关闭上传
   */
  public cancelUpload() {
    this.tableFindModel.getData(null, null);
    for (const dataMode of  this.tableFindModel2.dataModels) {
      dataMode['stopUpload'] = true;
    }
    this.tableFindModel2.setDataModels([], {total: 0});
    this.upload.isVisible = false;
  }

  /**
   * 选择目录
   */
  public uploadFolder() {
    const $this = this;
    const input = document.createElement('input');
    input.style.display = 'none';
    input.setAttribute('multiple', '');
    input.setAttribute('webkitdirectory', '');
    input.type = 'file';
    const event = document.createEvent('MouseEvents');
    event.initEvent('click', true, true); // 这里的click可以换成你想触发的行为
    input.dispatchEvent(event); // 这里的clickME可以换成你想触发行为的DOM结点
    input.onchange = (e) => {
      $this.uploadFolderFile(e.target['files'], $this.tableFindModel.formInfo['folderId']);
    };
  }

  /**
   * 选择文件
   */
  public selectFile() {
    const $this = this;
    const input = document.createElement('input');
    input.style.display = 'none';
    input.type = 'file';
    input.setAttribute('multiple', '');
    const event = document.createEvent('MouseEvents');
    event.initEvent('click', true, true); // 这里的click可以换成你想触发的行为
    input.dispatchEvent(event); // 这里的clickME可以换成你想触发行为的DOM结点
    input.onchange = (e) => {
      $this.uploadFolderFile(e.target['files'], $this.tableFindModel.formInfo['folderId']);
    };
  }

  /**
   * 上传文件数据处理
   */
  public async uploadFolderFile(files, folderId) {
    const myThis = this;
    const tokenInfo = new LoginDto(JSON.parse(CommonTool.getCookie(environment.tokenKey)));
    this.count = 0;
    this.upload.isVisible = true;
    const array = [];
    for (let i = 0; i < files.length; i++) {
      const file = files[i];
      // 获取文件目录
      let folderPath = '';
      if (file.webkitRelativePath !== '' && file.webkitRelativePath !== null && file.webkitRelativePath !== undefined) {
        const arrayPath = file.webkitRelativePath.split('/');
        arrayPath.pop();
        folderPath = arrayPath.join('/');
      }
      // 封装数据
      const dataModel = {
        data: {
          index: i,
          name: file.name,
          size: CommonTool.getSpace(file.size),
          progress: '0'
        },
        progress: {}
      };
      dataModel['upload'] = new XcUploadTool({
        uploadUrl: this.fileManagementService.getUploadUrl(), // 上传的url地址
        token: {
          token: tokenInfo.accessToken, // 用户token
          folderId: folderId ? folderId : '' // 文件夹id
        },
        isHttp: true,
        loadingStart: (current: number, data: UploadData, that: XcUploadTool) => {
          // 开始加载（计算md5）
          // current：当前进度
          // data：传入的数据对象
          // that: this对象
          const COMMON_TYPE = TableFindModel.COMMON_TYPE;
          dataModel['progress']['type'] = COMMON_TYPE.progress.TEXT;
          dataModel.data['progress'] = '0';
        },
        loadingProgress: (current, data, that) => {
          // 加载（计算md5）进度
          // current：当前进度
          // data：传入的数据对象
          // that: this对象
          // debugger;
          const progress = parseInt((current / (data.file.size / 100)) + '');
          dataModel.data['progress'] = progress + '%';
        },
        loadingSuccess: (md5, current, data, that) => {
          // 加载（计算md5）完成
          // md5：计算出的md5
          // data：传入的数据对象
          // current：当前进度
          // that: this对象
          dataModel.data['progress'] = '100%';
        },
        uploadStart: (current, data, that) => {
          // 开始上传
          // current：当前进度
          // data：传入的数据对象
          // that: this对象
          dataModel['progress']['status'] = 'active';
          const COMMON_TYPE = TableFindModel.COMMON_TYPE;
          dataModel['progress']['type'] = COMMON_TYPE.progress.PROGRESS;
        },
        uploadProgress: (current, data, that) => {
          // 上传进度
          // current：当前进度
          // data：传入的数据对象
          // that: this对象
          dataModel['progress']['status'] = 'active';
          dataModel.data['progress'] = Math.round(current / data.file.size * 100) + '';
        },
        uploadSuccess: (current, data, res, that) => {
          // 上传完成
          // current：当前进度
          // data：传入的数据对象
          // res：请求返回对象
          // that: this对象
          myThis.count++;
          dataModel['progress']['fastFinish'] = res['fastFinish'];
          dataModel['progress']['status'] = 'success';
          dataModel['operateStop'] = false;
          dataModel.data['progress'] = '100';
        },
        uploadFail: (data, res, that) => {
          // 上传失败
          // current：当前进度
          // data：传入的数据对象
          // res：请求返回对象
          // that: this对象
          dataModel['progress']['status'] = 'exception';
          dataModel['operateRetry'] = true;
          const message = res ? JSON.parse(res).message : '';
          myThis.message.error('上传失败：' + message);
        }
      }, {file, folderPath, resumed: true});
      array.push(dataModel);
    }
    this.tableFindModel2.initDataModels(array, {total: array.length});
    for (const dataModel of array) {
      await dataModel['upload'].asyncUpload().then(() => {
        console.log('上传完成！');
      });
    }
  }

  /**
   * 复制
   */
  public copy(data) {
    const input = document.createElement('input');
    input.value = data;
    document.body.appendChild(input);
    input.select();
    if (document.execCommand('Copy')) {
      this.message.success('复制成功！');
    } else {
      this.message.success('复制失败！');
    }
    input.style.display = 'none';
  }
}
